#include"server.h"
#include"base.qpb.h"
#include"user.qpb.h"
#include"gateway.qpb.h"
#include"friend.qpb.h"
#include"file.qpb.h"
#include"message_storage.qpb.h"
#include"message_transmit.qpb.h"
#include"speech_recognition.qpb.h"
#include"notify.qpb.h"


#include<QDebug>

//////////////////////////////////////////////////
////// 一些辅助函数
//////////////////////////////////////////////////

//生成默认的UserInfo对象
bite_im::UserInfo makeUserInfo(int index,const QByteArray& avatar){
    bite_im::UserInfo userInfo;
    userInfo.setUserId(QString::number((1000 + index)));
    userInfo.setNickname("张三" + QString::number(index));
    userInfo.setDescription("个性签名" + QString::number(index));
    userInfo.setPhone("18612345678");
    userInfo.setAvatar(avatar);
    return userInfo;
}

//生成默认的MessageInfo对象，文本消息
bite_im::MessageInfo makeTextMessageInfo(int index,const QString& chatSessionId,const QByteArray& avatar){
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index,avatar));

    bite_im::StringMessageInfo stringMessageInfo;
    stringMessageInfo.setContent("这是一条消息" + QString::number(index));

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::STRING);
    messageContent.setStringMessage(stringMessageInfo);

    messageInfo.setMessage(messageContent);

    return messageInfo;
}

bite_im::MessageInfo makeImageMessageInfo(int index,const QString& chatSessionId,const QByteArray& avatar){
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index,avatar));

    bite_im::ImageMessageInfo imageMessageInfo;
    imageMessageInfo.setFileId("testImage");
    //真实服务器推送的消息数据里,本身也不带图片的正文,只是fileId
    //需要通过fileId,二次发送请求,通过getSingleFile接口来获取消息
    //imageMessageInfo.setImageContent()

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::IMAGE);
    messageContent.setImageMessage(imageMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

bite_im::MessageInfo makeFileMessageInfo(int index,const QString& chatSessionId,const QByteArray& avatar){
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index,avatar));

    bite_im::FileMessageInfo fileMessageInfo;
    fileMessageInfo.setFileId("testFile");
    //真实服务器推送的消息数据里,本身也不带图片的正文,只是fileId
    //需要通过fileId,二次发送请求,通过getSingleFile接口来获取消息
    fileMessageInfo.setFileName("test.txt");
    fileMessageInfo.setFileSize(0);

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::FILE);
    messageContent.setFileMessage(fileMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

bite_im::MessageInfo makeSpeechMessageInfo(int index,const QString& chatSessionId,const QByteArray& avatar){
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index,avatar));

    bite_im::SpeechMessageInfo speechMessageInfo;
    speechMessageInfo.setFileId("testFile");
    //真实服务器推送的消息数据里,本身也不带图片的正文,只是fileId
    //需要通过fileId,二次发送请求,通过getSingleFile接口来获取消息
    speechMessageInfo.setFileId("testSpeech");

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::SPEECH);
    messageContent.setSpeechMessage(speechMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}


//////////////////////////////////////////////////
////// HTTP服务器
//////////////////////////////////////////////////

HttpServer* HttpServer::instance = nullptr;


HttpServer::HttpServer()
{

}

HttpServer *HttpServer::getInstance()
{
    if(instance == nullptr){
        instance = new HttpServer();
    }
    return instance;
}

bool HttpServer::init()
{
    //返回的值是int,表示成功绑定的端口号的数值
    int ret = httpServer.listen(QHostAddress::Any,8000);

    //配置路由
    httpServer.route("/ping",[](const QHttpServerRequest& req){
        (void) req;
        LOG() << "[http]收到ping请求";
        return "pong";
    });

    httpServer.route("/service/user/get_user_info",[=](const QHttpServerRequest& req){
        return this->getUserInfo(req);
    });

    httpServer.route("/service/friend/get_friend_list",[=](const QHttpServerRequest& req){
        return this->getFriendList(req);
    });

    httpServer.route("/service/friend/get_chat_session_list",[=](const QHttpServerRequest& req){
        return this->getChatSessionList(req);
    });

    httpServer.route("/service/friend/get_pending_friend_events",[=](const QHttpServerRequest& req){
        return this->getApplyList(req);
    });

    httpServer.route("/service/message_storage/get_recent",[=](const QHttpServerRequest& req){
        return this->getRecent(req);
    });

    httpServer.route("/service/message_transmit/new_message",[=](const QHttpServerRequest& req){
        return this->newMessage(req);
    });

    httpServer.route("/service/user/set_nickname",[=](const QHttpServerRequest& req){
        return this->setNickname(req);
    });

    httpServer.route("/service/user/set_description",[=](const QHttpServerRequest& req){
        return this->setDesc(req);
    });

    httpServer.route("/service/user/get_phone_verify_code",[=](const QHttpServerRequest& req){
        return this->getPhoneVerifyCode(req);
    });

    httpServer.route("/service/user/set_phone",[=](const QHttpServerRequest& req){
        return this->setPhone(req);
    });

    httpServer.route("/service/user/set_avatar",[=](const QHttpServerRequest& req){
        return this->setAvatar(req);
    });

    httpServer.route("/service/friend/remove_friend",[=](const QHttpServerRequest& req){
        return this->removeFriend(req);
    });

    httpServer.route("/service/friend/add_friend_apply",[=](const QHttpServerRequest& req){
        return this->addFriendApply(req);
    });

    httpServer.route("/service/friend/add_friend_process",[=](const QHttpServerRequest& req){
        return this->addFriendProcess(req);
    });

    httpServer.route("/service/friend/create_chat_session",[=](const QHttpServerRequest& req){
        return this->createChatSession(req);
    });

    httpServer.route("/service/friend/get_chat_session_member",[=](const QHttpServerRequest& req){
        return this->getChatSessionMember(req);
    });

    httpServer.route("/service/group/set_name",[=](const QHttpServerRequest& req){
        return this->sendNewGroupName(req);
    });

    httpServer.route("/service/friend/search_friend",[=](const QHttpServerRequest& req){
        return this->searchFriend(req);
    });

    httpServer.route("/service/message_storage/search_history",[=](const QHttpServerRequest& req){
        return this->searchHistory(req);
    });

    httpServer.route("/service/message_storage/get_history",[=](const QHttpServerRequest& req){
        return this->getHistory(req);
    });

    httpServer.route("/service/user/username_login",[=](const QHttpServerRequest& req){
        return this->usernameLogin(req);
    });

    httpServer.route("/service/user/username_register",[=](const QHttpServerRequest& req){
        return this->usernameRegister(req);
    });

    httpServer.route("/service/user/phone_login",[=](const QHttpServerRequest& req){
        return this->phoneLogin(req);
    });

    httpServer.route("/service/user/phone_register",[=](const QHttpServerRequest& req){
        return this->phoneRegister(req);
    });

    httpServer.route("/service/file/get_single_file",[=](const QHttpServerRequest& req){
        return this->getSingleFile(req);
    });

    httpServer.route("/service/speech/recognition",[=](const QHttpServerRequest& req){
        return this->recognition(req);
    });

    return ret == 8000;
}

QHttpServerResponse HttpServer::getUserInfo(const QHttpServerRequest &req)
{
    //解析请求，把req的body取出来，并通过pb进行反序列化
    bite_im::GetUserInfoReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取用户信息] requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    //构造响应数据
    bite_im::GetUserInfoRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    bite_im::UserInfo userInfo;
    userInfo.setUserId("1029");			//调整自己的用户id,和返回的消息列表的内容匹配上
    userInfo.setNickname("张三");
    userInfo.setDescription("这是个性签名");
    userInfo.setPhone("18612345678");
    userInfo.setAvatar(loadFiletoByteArray(":/resource/image/groupAvatar.png"));
    pbResp.setUserInfo(userInfo);

    QByteArray body = pbResp.serialize(&serializer);

    //构造HTTP响应数据
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getFriendList(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetFriendListReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取好友列表]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    //构造响应
    bite_im::GetFriendListRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //从文件读取数据操作，其实是比较耗时的
    //耗时操作如果放在循环内部，就会使得整个响应处理时间更长
    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 20;++i){
        //设置userInfo中的值
        bite_im::UserInfo userInfo = makeUserInfo(i,avatar);
        pbRsp.friendList().push_back(userInfo);
    }

    //进行序列化
    QByteArray body = pbRsp.serialize(&serializer);

    //构成HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionList(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetChatSessionListReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取好友申请列表]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    //构造响应
    bite_im::GetChatSessionListRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //构造若干单聊会话
    //从文件读取数据操作，其实是比较耗时的
    //耗时操作如果放在循环内部，就会使得整个响应处理时间更长
    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;++i){
        bite_im::ChatSessionInfo chatSessionInfo;
        chatSessionInfo.setChatSessionId(QString::number(2000 + i));
        chatSessionInfo.setChatSessionName("会话" + QString::number(i));
        chatSessionInfo.setSingleChatFriendId(QString::number(1000 + i));
        chatSessionInfo.setAvatar(avatar);

        bite_im::MessageInfo messageInfo = makeTextMessageInfo(i,chatSessionInfo.chatSessionId(),avatar);

        chatSessionInfo.setPrevMessage(messageInfo);

        pbRsp.chatSessionInfoList().push_back(chatSessionInfo);
    }

    //构造群聊会话
    QByteArray groupAvatar = loadFiletoByteArray(":/resource/image/groupAvatar.png");
    bite_im::ChatSessionInfo chatSessionInfo;
    chatSessionInfo.setChatSessionId(QString::number(2100));
    chatSessionInfo.setChatSessionName("会话" + QString::number(2100));
    chatSessionInfo.setSingleChatFriendId("");
    chatSessionInfo.setAvatar(groupAvatar);
    bite_im::MessageInfo messageInfo = makeTextMessageInfo(0,chatSessionInfo.chatSessionId(),avatar);
    chatSessionInfo.setPrevMessage(messageInfo);
    pbRsp.chatSessionInfoList().push_back(chatSessionInfo);


    //进行序列化
    QByteArray body = pbRsp.serialize(&serializer);

    //构成HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::getApplyList(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetPendingFriendEventListReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取好友申请列表]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId();

    //构造响应
    bite_im::GetPendingFriendEventListRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //从文件读取数据操作，其实是比较耗时的
    //耗时操作如果放在循环内部，就会使得整个响应处理时间更长
    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;++i){
        bite_im::FriendEvent friendEvent;
        friendEvent.setEventId(""); 	//此处不再使用这个eventId,直接设为""
        friendEvent.setSender(makeUserInfo(i,avatar));

        pbRsp.event().push_back(friendEvent);
    }

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getRecent(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetRecentMsgReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取好友申请列表]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ", chatSessioId=" << pbReq.chatSessionId();

    //构造响应
    bite_im::GetRecentMsgRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //从文件读取数据操作，其实是比较耗时的
    //耗时操作如果放在循环内部，就会使得整个响应处理时间更长
    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;++i){
        bite_im::MessageInfo messageInfo = makeTextMessageInfo(i,"2000",avatar);

        pbRsp.msgList().push_back(messageInfo);
    }
    bite_im::MessageInfo imagemessageInfo = makeImageMessageInfo(30,"2000",avatar);
    pbRsp.msgList().push_back(imagemessageInfo);
    bite_im::MessageInfo filemessageInfo = makeFileMessageInfo(31,"2000",avatar);
    pbRsp.msgList().push_back(filemessageInfo);
    bite_im::MessageInfo speechmessageInfo = makeSpeechMessageInfo(32,"2000",avatar);
    pbRsp.msgList().push_back(speechmessageInfo);

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::newMessage(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::NewMessageReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ发送消息]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ", chatSessioId=" << pbReq.chatSessionId();

    if(pbReq.message().messageType() == bite_im::MessageTypeGadget::MessageType::STRING){
        LOG() << "发送的消息内容=" << pbReq.message().stringMessage().content();
    }

    //构造响应
    bite_im::NewMessageRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::setNickname(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::SetUserNicknameReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ发送消息]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ", nickname=" << pbReq.nickname();

    //构造响应
    bite_im::SetUserNicknameRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;


}

QHttpServerResponse HttpServer::setDesc(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::SetUserDescriptionReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ发送消息]requestId=" << pbReq.requestId() << ",loginSessionId=" << pbReq.sessionId()
          << ", desc=" << pbReq.description();

    //构造响应
    bite_im::SetUserDescriptionRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::getPhoneVerifyCode(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::PhoneVerifyCodeReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取短信验证码]requestId=" << pbReq.requestId() << ", phone" << pbReq.phoneNumber();

    //构造响应
    bite_im::PhoneVerifyCodeRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setVerifyCodeId("testVerifyCodeId");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::setPhone(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::SetUserPhoneNumberReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ修改手机号]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", phone=" << pbReq.phoneNumber() << ", verifyCodeId=" << pbReq.phoneVerifyCodeId() << ", verifyCode=" << pbReq.phoneVerifyCode();

    //构造响应
    bite_im::SetUserPhoneNumberRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造HTTP响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::setAvatar(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::SetUserAvatarReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ修改头像]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    //构造响应
    bite_im::SetUserAvatarRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::removeFriend(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::FriendRemoveReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ修改头像]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    //构造响应
    bite_im::FriendRemoveRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::addFriendApply(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::FriendAddReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ添加好友申请]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", userId=" << pbReq.requestId();

    //构造响应
    bite_im::FriendAddRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setNotifyEventId("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::addFriendProcess(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::FriendAddProcessReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ添加好友申请处理]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", applyUserId=" << pbReq.applyUserId() << ", agree=" << pbReq.agree();

    //构造响应
    bite_im::FriendAddProcessRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setNewSessionId("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::createChatSession(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::ChatSessionCreateReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ创建会话]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", userIdList=" << pbReq.memberIdList();

    //构造响应
    bite_im::FriendAddProcessRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setNewSessionId("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionMember(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetChatSessionMemberReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ创建会话]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSesssionId=" << pbReq.chatSessionId();

    //构造响应
    bite_im::GetChatSessionMemberRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //循环的构造多个userInfo,添加到memeberInfoList中
    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 10;i++){
        bite_im::UserInfo userInfo = makeUserInfo(i,avatar);
        pbRsp.memberInfoList().push_back(userInfo);
    }

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::sendNewGroupName(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::SetGroupNameReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ修改群聊名称]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId();

    //构造响应
    bite_im::SetGroupNameRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::searchFriend(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::FriendSearchReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ搜索好友]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", searchKey==" << pbReq.searchKey();

    //构造响应
    bite_im::FriendSearchRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;i++){
        bite_im::UserInfo userInfo = makeUserInfo(i,avatar);
        pbRsp.userInfo().push_back(userInfo);
    }

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::searchHistory(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::MsgSearchReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ搜索好友]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId() << ", searchKey=" << pbReq.searchKey();

    //构造响应
    bite_im::MsgSearchRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;i++){
        bite_im::MessageInfo message = makeTextMessageInfo(i,pbReq.chatSessionId(),avatar);
        pbRsp.msgList().push_back(message);
    }
    //构造图片消息
    bite_im::MessageInfo message = makeImageMessageInfo(10,pbReq.chatSessionId(),avatar);
    pbRsp.msgList().push_back(message);
    //构造文件消息
    message = makeFileMessageInfo(11,pbReq.chatSessionId(),avatar);
    pbRsp.msgList().push_back(message);
    //构造语音消息
    message = makeSpeechMessageInfo(12,pbReq.chatSessionId(),avatar);
    pbRsp.msgList().push_back(message);


    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getHistory(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetHistoryMsgReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ按时间搜索历史消息]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId() << ", begTime=" << pbReq.startTime() << ", endTime=" << pbReq.overTime();

    //构造响应
    bite_im::MsgSearchRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;i++){
        bite_im::MessageInfo message = makeTextMessageInfo(i,pbReq.chatSessionId(),avatar);
        pbRsp.msgList().push_back(message);
    }

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::usernameLogin(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::UserLoginReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ用户名密码登录]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.nickname()
          << ", password=" << pbReq.password();

    //构造响应
    bite_im::UserLoginRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setLoginSessionId("testLoginSessionId");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::usernameRegister(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::UserRegisterReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ用户名密码注册]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.nickname()
          << ", password=" << pbReq.password();

    //构造响应
    bite_im::UserRegisterRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::phoneLogin(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::PhoneLoginReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ手机号码注册]requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();

    //构造响应
    bite_im::PhoneLoginRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setLoginSessionId("testLoginSessionId");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::phoneRegister(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::PhoneRegisterReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ手机号码注册]requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();

    //构造响应
    bite_im::PhoneRegisterRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;

}

QHttpServerResponse HttpServer::getSingleFile(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::GetSingleFileReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ获取单个文件]requestId=" << pbReq.requestId() << ", fileId=" << pbReq.fileId();

    //构造响应
    bite_im::GetSingleFileRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");

    bite_im::FileDownloadData fileDownloadData;
    fileDownloadData.setFileId(pbReq.fileId());
    //此处后续要能够支持桑情况,图片文件、普通文件、语音文件
    //直接使用fileId做区分
    if(pbReq.fileId() == "testImage"){
        fileDownloadData.setFileContent(loadFiletoByteArray(":/resource/image/logo.png"));
    }else if(pbReq.fileId() == "testFile"){
        fileDownloadData.setFileContent(loadFiletoByteArray(":/resource/file/test.txt"));
    }else if(pbReq.fileId() == "testSpeech"){
        //由于此处暂时还没有音频文件,得后面写了录音功能，才能生成
        fileDownloadData.setFileContent(loadFiletoByteArray(":/resource/file/speech.pcm"));
    }else{
        pbRsp.setSuccess(false);
        pbRsp.setErrmsg("fileId 不是预期的测试 fileId");
    }
    pbRsp.setFileData(fileDownloadData);

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::recognition(const QHttpServerRequest &req)
{
    //解析请求，把req的body拿出来
    bite_im::SpeechRecognitionReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ语音转文字]requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    //构造响应
    bite_im::SpeechRecognitionRsp pbRsp;
    pbRsp.setRequestId(pbRsp.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setRecognitionResult("你好你好,这是一段语音消息,你好您好,这是一段语音消息");

    //序列化成字节数组
    QByteArray body = pbRsp.serialize(&serializer);

    //构造响应对象
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","application/x-protobuf");
    return httpResp;
}



//////////////////////////////////////////////////
////// WebSocket服务器
//////////////////////////////////////////////////
WebsocketServer* WebsocketServer::instance = nullptr;


WebsocketServer *WebsocketServer::getInstance()
{
    if(instance == nullptr){
        instance = new WebsocketServer();
    }
    return instance;
}

//针对websocket服务器进行初始化操作
bool WebsocketServer::init()
{
    //1.连接信号槽
    connect(&websocketServer,&QWebSocketServer::newConnection,this,[=](){
        //连接建立成功之后
        LOG() << "[websocket]连接建立成功!";

        //获取到用来通信的socket对象，nextPendingConnection 类似于原生socket中的accept
        QWebSocket* socket = websocketServer.nextPendingConnection();

        //针对这个socket对象，进行剩余信号处理
        //连接断开
        connect(socket,&QWebSocket::disconnected,this,[=](){
            LOG() << "[websocket]连接断开!";
            //确保客户端断开连接，重新连接之后不会触发程序崩溃的问题
            disconnect(this,&WebsocketServer::sendTextResp,this,nullptr);
            disconnect(this,&WebsocketServer::sendImageResp,this,nullptr);
            disconnect(this,&WebsocketServer::sendFriendRemove,this,nullptr);
            disconnect(this,&WebsocketServer::sendAddFriendApply,this,nullptr);
            disconnect(this,&WebsocketServer::sendAddFriendProcess,this,nullptr);
            disconnect(this,&WebsocketServer::sendCreateChatSession,this,nullptr);
            disconnect(this,&WebsocketServer::sendFileResp,this,nullptr);
            disconnect(this,&WebsocketServer::sendSpeechResp,this,nullptr);
        });

        //连接出错
        connect(socket,&QWebSocket::errorOccurred,this,[=](QAbstractSocket::SocketError error){
            LOG() << "[websocket]连接出错!" << error;
        });

        //收到文本数据
        connect(socket,&QWebSocket::textMessageReceived,this,[=](const QString&message){
            LOG() << "[websocket]收到文本消息!" << message;
        });

        //收到二进制数据
        connect(socket,&QWebSocket::binaryMessageReceived,this,[=](const QByteArray& byteArray){
            LOG() << "[websocket]收到二进制消息!" << byteArray;
        });

        connect(this,&WebsocketServer::sendTextResp,this,[=](){
            //此处就可以捕获到socket对象，从而可以通过socket对象给客户端返回数据
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket 对象无效!";
                return ;
            }

            //构造数据
            QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
            bite_im::MessageInfo messageInfo = makeTextMessageInfo(this->messageIndex++,"2000",avatar);

            bite_im::NotifyNewMessage notifyNewMessage;
            notifyNewMessage.setMessageInfo(messageInfo);

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);
            notifyMessage.setNewMessageInfo(notifyNewMessage);

            //序列化
            QByteArray body = notifyMessage.serialize(&this->serializer);

            //发送消息给客户端
            socket->sendBinaryMessage(body);

            LOG() << "发送文本消息响应" << messageInfo.message().stringMessage().content();

        });

        connect(this,&WebsocketServer::sendImageResp,this,[=](){
            //此处就可以捕获到socket对象，从而可以通过socket对象给客户端返回数据
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket 对象无效!";
                return ;
            }

            //构造数据
            QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
            bite_im::MessageInfo messageInfo = makeFileMessageInfo(this->messageIndex++,"2000",avatar);

            bite_im::NotifyNewMessage notifyNewMessage;
            notifyNewMessage.setMessageInfo(messageInfo);

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);
            notifyMessage.setNewMessageInfo(notifyNewMessage);

            //序列化
            QByteArray body = notifyMessage.serialize(&this->serializer);

            //发送消息给客户端
            socket->sendBinaryMessage(body);

            LOG() << "发送文件消息响应";

        });

        connect(this,&WebsocketServer::sendFileResp,this,[=](){
            //此处就可以捕获到socket对象，从而可以通过socket对象给客户端返回数据
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket 对象无效!";
                return ;
            }

            //构造数据
            QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
            bite_im::MessageInfo messageInfo = makeFileMessageInfo(this->messageIndex++,"2000",avatar);

            bite_im::NotifyNewMessage notifyNewMessage;
            notifyNewMessage.setMessageInfo(messageInfo);

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);
            notifyMessage.setNewMessageInfo(notifyNewMessage);

            //序列化
            QByteArray body = notifyMessage.serialize(&this->serializer);

            //发送消息给客户端
            socket->sendBinaryMessage(body);

            LOG() << "发送文件消息响应";

        });

        connect(this,&WebsocketServer::sendSpeechResp,this,[=](){
            //此处就可以捕获到socket对象，从而可以通过socket对象给客户端返回数据
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket 对象无效!";
                return ;
            }

            //构造数据
            QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
            bite_im::MessageInfo messageInfo = makeSpeechMessageInfo(this->messageIndex++,"2000",avatar);

            bite_im::NotifyNewMessage notifyNewMessage;
            notifyNewMessage.setMessageInfo(messageInfo);

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);
            notifyMessage.setNewMessageInfo(notifyNewMessage);

            //序列化
            QByteArray body = notifyMessage.serialize(&this->serializer);

            //发送消息给客户端
            socket->sendBinaryMessage(body);

            LOG() << "发送文件消息响应";

        });

        connect(this,&WebsocketServer::sendFriendRemove,this,[=](){
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket 对象无效";
            }

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::FRIEND_REMOVE_NOTIFY);

            bite_im::NotifyFriendRemove notifyFriendRemove;
            notifyFriendRemove.setUserId("1000");
            notifyMessage.setFriendRemove(notifyFriendRemove);

            QByteArray body = notifyMessage.serialize(&serializer);
            socket->sendBinaryMessage(body);
            LOG() << "通知对方好友被删除 userId=1000";
        });

        connect(this,&WebsocketServer::sendAddFriendApply,this,[=](){
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket 对象无效";
            }

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::FRIEND_ADD_APPLY_NOTIFY);

            QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
            bite_im::UserInfo userInfo = makeUserInfo(100,avatar);

            bite_im::NotifyFriendAddApply friendAddAply;
            friendAddAply.setUserInfo(userInfo);

            notifyMessage.setFriendAddApply(friendAddAply);

            QByteArray body = notifyMessage.serialize(&serializer);
            socket->sendBinaryMessage(body);
            LOG() << "通知对方好友申请数据";

        });

        connect(this,&WebsocketServer::sendAddFriendProcess,this,[=](bool agree){
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket对象无效";
            }

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");
            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::FRIEND_ADD_PROCESS_NOTIFY);

            QByteArray avatar = loadFiletoByteArray(":/resource/image/defaultAvatar.png");
            bite_im::UserInfo userInfo = makeUserInfo(100,avatar);

            bite_im::NotifyFriendAddProcess friendProcess;
            friendProcess.setUserInfo(userInfo);
            friendProcess.setAgree(agree);

            notifyMessage.setFriendProcessResult(friendProcess);

            QByteArray body = notifyMessage.serialize(&serializer);
            socket->sendBinaryMessage(body);
            LOG() << "通知好友申请的处理结果 userId=" << userInfo.userId() << ", agree=" << agree;
        });

        connect(this,&WebsocketServer::sendCreateChatSession,this,[=](){
            if(socket == nullptr || !socket->isValid()){
                LOG() << "socket对象无效!";
                return;
            }
            QByteArray avatar = loadFiletoByteArray(":/resource/image/groupAvatar.png");

            bite_im::NotifyMessage notifyMessage;
            notifyMessage.setNotifyEventId("");

            notifyMessage.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_SESSION_CREATE_NOTIFY);

            bite_im::MessageInfo messageInfo = makeTextMessageInfo(0,"2100",avatar);

            bite_im::ChatSessionInfo chatSessionInfo;
            chatSessionInfo.setChatSessionId("2100");
            chatSessionInfo.setSingleChatFriendId("");
            chatSessionInfo.setChatSessionName("新的群聊");
            chatSessionInfo.setPrevMessage(messageInfo);
            chatSessionInfo.setAvatar(avatar);

            bite_im::NotifyNewChatSession newChatSession;
            newChatSession.setChatSessionInfo(chatSessionInfo);
            notifyMessage.setNewChatSessionInfo(newChatSession);

            //序列化操作
            QByteArray body = notifyMessage.serialize(&serializer);

            //通过websocket推送数据
            socket->sendBinaryMessage(body);
            LOG() << "创建会话!";
        });

        connect(this,&WebsocketServer::sendNewGroupName,this,[=](){});

    });

    //2.绑定端口,启动服务
    bool ok = websocketServer.listen(QHostAddress::Any,8001);
    return ok;
}
